#删除原容器
echo "begin stop and delete old docker container......"
docker stop blog-ui-im-cqj && docker rm blog-ui-im-cqj
echo "====================================================="


#拉取镜像
echo "begin pull base images from docker hub"
docker pull nginx
echo "pull images success!"
echo "====================================================="


echo "begin start all containers..."
# ui path
blog_ui_path="/home/docker/sara-blog/webroot/sara-blog-vue2"
if [[ ! -d "$blog_ui_path" ]]; then
	echo "blog_ui_path webroot path not exsits,create now."
	mkdir -p ${blog_ui_path}
else
	rm -rf /home/docker/sara-blog/webroot/sara-blog-vue2/*
	echo "blog_ui_path webroot path exsits,do not create and remove all resource"
fi
# im path
blog_im_path="/home/docker/sara-blog/webroot/sara-chat-vue3"
if [[ ! -d "$blog_im_path" ]]; then
	echo "blog_im_path webroot path not exsits,create now."
	mkdir -p ${blog_im_path}
else
	rm -rf /home/docker/sara-blog/webroot/sara-chat-vue3/*
	echo "blog_im_path webroot path exsits,do not create and remove all resource"
fi

# 打包后的资源拷贝
pwd
cp -rf sara-blog-vue2/dist/* /home/docker/sara-blog/webroot/sara-blog-vue2/
cp -rf sara-chat-vue3/dist/* /home/docker/sara-blog/webroot/sara-chat-vue3/

# 先把容器运行起来
docker run -d \
--network=bridge-chen \
-p 80:80 \
-p 443:443 \
-p 9003:9003 \
-p 9324:9324 \
--restart=always \
--privileged=true \
--name='blog-ui-im-cqj' \
nginx:latest

# 拷贝容器内的文件到博客文件目录
docker cp blog-ui-im-cqj:/etc/nginx/ /home/docker/sara-blog/conf
docker cp blog-ui-im-cqj:/usr/share/nginx/ /home/docker/sara-blog/webroot
# 删除原来的nginx文件，并替换为新的
rm -rf /home/docker/sara-blog/conf/conf.d/*
cp -r sara-blog-vue2/nginx/* /home/docker/sara-blog/conf/conf.d
# 替换后删除原容器
docker stop blog-ui-im-cqj && docker rm blog-ui-im-cqj

# 启动新的容器
docker run -d \
--ip 172.20.1.13 \
--network=bridge-chen \
-p 80:80 \
-p 443:443 \
-p 9003:9003 \
-p 9324:9324 \
-v '/home/docker/sara-blog/webroot:/usr/share/nginx' \
-v '/home/docker/sara-blog/conf:/etc/nginx' \
--restart=always \
--privileged=true \
--name='blog-ui-im-cqj' \
nginx:latest
echo '===============start all containers...ok=============='

